package com.sxu.demo;

import java.time.LocalTime;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/**
 * 任务调度线程池 —— 定时执行
 */
public class Test8 {
    public static void main(String[] args) {
        ScheduledExecutorService pool = Executors.newScheduledThreadPool(1);
        System.out.println("start....." + LocalTime.now().toString());
        // 固定速率执行任务
//        pool.scheduleAtFixedRate(()-> {
//            System.out.println("running....." + LocalTime.now().toString());
//        },1,1, TimeUnit.SECONDS);

        pool.scheduleWithFixedDelay(()-> {
            System.out.println("running....." + LocalTime.now().toString());
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        },1,1, TimeUnit.SECONDS);
    }

}
